import java.util.concurrent.*;

public class ClosedLock {

    CountDownLatch countDownLatch = new CountDownLatch(5);
    public void testCountDownLatch () throws Exception {
        for (int i=0; i < 5; i++) {
            new Thread(() -> {
                countDownLatch.countDown();
            }).start();
        }
        countDownLatch.await();
    }

    CyclicBarrier cyclicBarrier = new CyclicBarrier(5, () -> {/*任务完成*/});
    public void testCyclicBarrier () {
        for (int i=0; i < 5; i++) {
            new Thread(() -> {
                try {
                    cyclicBarrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

    Semaphore semaphore = new Semaphore(3);
    public void testSemaphore () {
        for (int i=0; i < 6; i++) {
            new Thread(() -> {
                try {
                    semaphore.acquire();
                    TimeUnit.SECONDS.sleep(3);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    semaphore.release();
                }
            }).start();
        }
    }
}
